package com.thesis.manage.service.impl;

import com.github.crab2died.ExcelUtils;
import com.github.pagehelper.PageInfo;
import com.thesis.manage.common.constant.YesOrNoEnum;
import com.thesis.manage.common.exception.CommonException;
import com.thesis.manage.common.service.impl.BaseServiceImpl;
import com.thesis.manage.domain.Classes;
import com.thesis.manage.domain.College;
import com.thesis.manage.domain.Major;
import com.thesis.manage.domain.Student;
import com.thesis.manage.mapper.StudentMapper;
import com.thesis.manage.service.ClassesService;
import com.thesis.manage.service.CollegeService;
import com.thesis.manage.service.MajorService;
import com.thesis.manage.service.StudentService;
import com.thesis.manage.service.constant.ErrorMessage;
import com.thesis.manage.service.model.UserActivityStateEnum;
import com.thesis.manage.service.module.excel.StudentExcel;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.common.Mapper;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author php
 * @date 2018/1/23
 */
@Service
public class StudentServiceImpl extends BaseServiceImpl<Student> implements StudentService {
    @Resource
    private StudentMapper studentMapper;

    @Resource
    private CollegeService collegeService;

    @Resource
    private MajorService majorService;

    @Resource
    private ClassesService classesService;

    @Override
    protected Mapper<Student> getMapper() {
        return this.studentMapper;
    }

    @Override
    protected Class<Student> getEntityClass() {
        return Student.class;
    }

    /**
     * 验证学生是否存在
     *
     * @param studentNumber 学生序列号
     * @return boolean
     */
    private boolean isNotExist(String studentNumber) {
        return this.findByStudentNumber(studentNumber) == null;
    }

    @Override
    public Student findByStudentNumber(String studentNumber) {
        Assert.notNull(studentNumber, ErrorMessage.PARAMS_IS_NULL);
        Student student = new Student();
        student.setStudentNumber(studentNumber);
        student.setDelStatus(YesOrNoEnum.NO.getValue());
        return this.studentMapper.selectOne(student);
    }

    @Override
    public void importExcel(MultipartFile file) {
        // TODO: 2018/1/26 使用多线程去导入数据 （等待优化）
        Assert.notNull(file, ErrorMessage.REQUEST_SELECT_FILE);
        try {
            List<StudentExcel> studentExcels = ExcelUtils.getInstance().readExcel2Objects(file.getInputStream(), StudentExcel.class);
            /*
            对象转换
             */
            if (CollectionUtils.isNotEmpty(studentExcels)) {
                Student student;
                for (StudentExcel studentExcel : studentExcels) {
                    student = new Student();
                    PropertyUtils.copyProperties(student, studentExcel);
                    if (this.isNotExist(student.getStudentNumber())) {
                        College college = this.collegeService.findByName(student.getCollegeName());
                        if (college != null) {
                            student.setCollegeId(college.getId());
                        }
                        Major major = this.majorService.findByName(student.getMajorName());
                        if (major != null) {
                            student.setMajorId(major.getId());
                        }
                        Classes classes = this.classesService.findByName(student.getClassesName(), student.getMajorId());
                        if (classes != null) {
                            student.setClassId(classes.getId());
                        }
                        student.setPassword(DigestUtils.md5DigestAsHex(student.getIdCard().getBytes()));
                        student.setStatus(UserActivityStateEnum.OFF_LINE.getValue());
                        this.save(student);
                    }
                }
            }
        } catch (Exception e) {
            throw new CommonException("file import failed", ErrorMessage.FILE_IMPORT_FAILED);
        }
    }

    @Override
    public PageInfo<Student> listByCollege(String collegeName, Pageable pageable) {
        Student student = new Student();
        student.setCollegeName(collegeName);
        return this.list(student, pageable);
    }

    @Override
    public List<Student> list(Student student) {
        student.setDelStatus(YesOrNoEnum.NO.getValue());
        return this.studentMapper.select(student);
    }

    /**
     * 对象转换
     *
     * @param studentExcels 转换对象
     * @return list<Student>
     */
    private List<Student> convert(List<StudentExcel> studentExcels) {
        List<Student> list = new ArrayList<>();
        try {
            if (CollectionUtils.isNotEmpty(studentExcels)) {
                Student student;
                for (StudentExcel studentExcel : studentExcels) {
                    student = new Student();
                    PropertyUtils.copyProperties(student, studentExcel);
                    list.add(student);
                }
            }
            return list;
        } catch (Exception e) {
            throw new CommonException("object covert failed", ErrorMessage.OBJECT_CONVERT_FAILED);
        }
    }


}
